package com.example.demo.test;


import org.bouncycastle.util.encoders.Hex;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.*;

/**
 * AES 对称加密工具
 */
public class Encrypt {

    /**
     * 加密
     *
     * @param key 秘钥
     * @param src 要加密的数据
     * @return
     */
    public static String encode(String key, String src) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            byte[] keys = MessageDigest.getInstance("MD5").digest(key.getBytes("UTF-8"));
            keyGenerator.init(128, new SecureRandom(keys));
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] keyBytes = secretKey.getEncoded();
            //Key转换
            Key key1 = new SecretKeySpec(keyBytes, "AES");
            //加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key1);
            byte[] encodeResult = Hex.encode(cipher.doFinal(src.getBytes(Charset.forName("UTF-8"))));
            return new String(encodeResult);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 解密
     *
     * @param key 秘钥
     * @param src 要解密的数据
     * @return
     */
    public static String decode(String key, String src) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            byte[] keys = MessageDigest.getInstance("MD5").digest(key.getBytes("UTF-8"));
            keyGenerator.init(128, new SecureRandom(keys));
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] keyBytes = secretKey.getEncoded();
            Key key1 = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key1);
            byte[] decodeResult = cipher.doFinal(Hex.decode(src.getBytes(Charset.forName("UTF-8"))));
            return new String(decodeResult);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) throws Exception {
        String strKey = "aaaaaaaaaaaaaaaaaaaaaaaa";
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("AES");
        DESKeySpec keySpec = new DESKeySpec(strKey.getBytes("utf-8"));
        SecretKey secretKey = keyFactory.generateSecret(keySpec);
        Key key1 = new SecretKeySpec(secretKey.getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key1);
        byte[] encodeResult = Hex.encode(cipher.doFinal(strKey.getBytes(Charset.forName("UTF-8"))));
        System.out.println(new String(encodeResult));
    }
}

